<?php

namespace Frontend\Controllers;

use Models\CouponUser;
use Models\CouponCard;
use Models\Coupon;

class AppcouponController extends BaseController
{
    public function indexAction($id)
    {
        $open_id = $this->request->get('openid');
        $coupon = Coupon::findFirstByid($id);
        $this->view->setVar("coupon", $coupon);
        $this->view->setVar("id", $id);
        $this->view->setVar("openid", $open_id);
    }

    public function cardAction($id)
    {
        $now = date("Y-m-d", strtotime("now"));
        $coupon = Coupon::findFirst("id=" . $id . " AND del=0");
        $this->view->setVar("coupon", $coupon);
        if ($coupon) {
            if ($coupon->start <= $now and $now <= $coupon->end) {
                $open_id = $this->request->get('openid');
                $card = CouponUser::findFirst("pid=" . $id . "AND openid='" . $open_id . "'");
                if (!$card) {
                    $count = Couponcard::sum(array("pid=" . $id . "AND del=0", 'column' => 'count'));
                    if ($count == 0) return $this->view->setVar("nomore", 1);
                    $cards = CouponCard::find("pid=" . $id . "AND del=0");
                    $resultNum = rand(1, $count);
                    $item = 0;
                    foreach ($cards as $card) {
                        $item += $card->count;
                        if ($resultNum <= $item) {
                            $usercard = new CouponUser();
                            $usercard->openid = $open_id;
                            $usercard->cid = $card->id;
                            $usercard->unique = $this->getunique();
                            $usercard->pid = $id;
                            $usercard->del = 0;
                            if ($usercard->save()) {
                                $card->count = $card->count - 1;
                                $card->update();
                            }
                            echo "恭喜您获得一张优惠券";
                            $this->view->setVar("card", $usercard);
                            break;
                        }
                    }
                } else {
                    if ($card->del == 0) {
                        echo "你已经领取过了该活动的优惠券";
                    } else {
                        echo "该优惠券已经被使用";
                    }
                    $this->view->setVar("card", $card);
                }
            } else {
                echo "活动不在有效期";
            }
        } else {
            echo "活动不存在";
        }
    }

    private function getunique($len = 6, $format = 'NUMBER')
    {


        switch ($format) {
            case 'ALL':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
                break;
            case 'CHAR':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
                break;
            case 'NUMBER':
                $chars = '0123456789';
                break;
            default :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
                break;
        }
        mt_srand((double)microtime() * 1000000 * getmypid());
        $password = "";
        while (strlen($password) < $len)
            $password .= substr($chars, (mt_rand() % strlen($chars)), 1);
        return $password;
    }


}